package cas.ihep.util;

/**
 * Created by chocolate on 2017/11/24.
 */

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import java.io.IOException;

public class HepFileInputFormat extends FileInputFormat<BytesWritable, BytesWritable> {

    public HepFileInputFormat(){
    }

    @Override
    protected boolean isSplitable(JobContext context, Path filename){
        return false;
    }

    @Override
    public RecordReader<BytesWritable,BytesWritable> createRecordReader(InputSplit split,
                                                                        TaskAttemptContext context) throws IOException {
        return new RootFileRecordReader();
    }

    public static class RootFileRecordReader extends RecordReader<BytesWritable,BytesWritable> {
        private long startOffset;
        private long end;
        private long pos;
        private Path path;
        private BytesWritable key;
        private BytesWritable value;
        public void initialize(InputSplit split, TaskAttemptContext context)
                throws IOException, InterruptedException {
        }

        public void close() throws IOException { }

        public float getProgress() throws IOException { /*进度*/
            if (startOffset == end) {
                return 0.0f;
            } else {
                return Math.min(1.0f, (pos - startOffset) / (float)(end - startOffset));
            }
        }

        public boolean nextKeyValue() throws IOException {
            //TODO
            return false;
        }

        public BytesWritable getCurrentKey()
                throws IOException, InterruptedException {
            return key;
        }

        public BytesWritable getCurrentValue() throws IOException, InterruptedException {
            return value;
        }
    }
}

